package com.sky.controller.admin;

import java.io.IOException;
import java.util.List;

import com.sky.dto.GoodsSalesDTO;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.UserService;
import com.sky.service.WorkSpaceService;
import com.sky.utils.JwtUtil;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@Slf4j
@RequestMapping("/admin/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private WorkSpaceService workSpaceService;

    /**
     * 营业查询
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询营业额:{},{}",begin,end);
        TurnoverReportVO turnoverReport = reportService.getTurnoverReport(begin, end);
        return Result.success(turnoverReport);
    }

    /**
     * 查询用户总数
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询用户状态");
        return Result.success(userService.getUserStatistics(begin,end));
    }
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                  @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询用户状态");
        return Result.success( reportService.getOrderReport(begin,end));
    }

    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> saleTop10(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询热销菜品");
        return Result.success(reportService.getGoodsSales(begin,end));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        workSpaceService.exportBussinessData(response);
    }
}
